package com.tvein.sanguo.event.controller
{
	//flash
	import flash.events.Event;
    import flash.net.URLRequest;
    import flash.media.Sound;
    import flash.media.SoundTransform;
	//pureMVC
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.command.SimpleFabricationCommand;
	//custom
	import com.tvein.sanguo.event.common.GameConstants;
	import com.tvein.sanguo.event.model.SystemProxy;
	import com.tvein.sanguo.event.view.EventMediator;
//
	public class EventRun extends SimpleFabricationCommand implements ICommand
	{
		private var eventMediator:EventMediator;
		private var systemData:SystemProxy;
		
		private var eventSound:Sound;
		
		override public function execute(notification:INotification) : void
		{
			eventMediator = facade.retrieveMediator(EventMediator.NAME) as EventMediator;
			systemData = facade.retrieveProxy(SystemProxy.NAME) as SystemProxy;
			
			var i:int;
			
			switch(notification.getBody()[0])
			{
				case "start":
					if ((systemData.curEvent == "") && (systemData.getScript().length > 0))
					{
						systemData.curProcess = 0;
						
						for (i = 0; i < systemData.getScript().length; i++)
						{
							if (systemData.getScript()[i].Main.room)
							{
								if (systemData.roomName == systemData.getScript()[i].Main.room)
								{
									systemData.curEvent = systemData.getScript()[i].Name;
									sendNotification(GameConstants.EVENT_RUN, ["play"]);
									break;
								}
							}
							else
							{
								systemData.curEvent = systemData.getScript()[i].Name;
								sendNotification(GameConstants.EVENT_RUN, ["play"]);
								break;
							}
						}
					}
					
					break;
				case "play":
					for (i = 0; i < systemData.getScript().length; i++)
					{
						if (systemData.curEvent == systemData.getScript()[i].Name)
						{
							break;
						}
					}
					
					var proc:Object = systemData.getScript()[i].Process;
					var allRole:Object = systemData.getScript()[i].AllRole;
					var next:Boolean = false;
					
					if (proc.length > 0)
					{
						switch(proc[0].type)
						{
							case "movie":
								eventMediator.eventShell.initMovie("movie/" + proc[0].swf);
								proc.shift();
								break;
							case "effect":
								eventSound = new Sound();
								eventSound.addEventListener(Event.COMPLETE, _onSoundLoaded);
								eventSound.load(new URLRequest(systemData.srcServer + "/SanGuo/sound/effect/" + proc[0].file));
								proc.shift();
								break;
							case "speak":
								for (i = 0; i < allRole.length; i++)
								{
									if (allRole[i].id == proc[0].role)
									{
										eventMediator.eventShell.initSpeak(allRole[i], proc[0].say);
										break;
									}
								}
								
								if (proc.length == 1)
								{
									eventMediator.eventShell.talk.mcNext.visible = false;
								}
								else
								{
									eventMediator.eventShell.talk.mcNext.visible = true;
								}
								proc.shift();
								break;
							case "tips":
								for (i = 0; i < allRole.length; i++)
								{
									if (allRole[i].id == proc[0].role)
									{
										eventMediator.eventShell.initTips(allRole[i], proc[0].say, "tips/" + proc[0].show);
										break;
									}
								}
								
								if (proc.length == 1)
								{
									eventMediator.eventShell.talk.mcNext.visible = false;
								}
								else
								{
									eventMediator.eventShell.talk.mcNext.visible = true;
								}
								proc.shift();
								break;
							case "menu":
								//say
								for (i = 0; i < allRole.length; i++)
								{
									if (allRole[i].id == proc[0].role)
									{
										eventMediator.eventShell.initSpeak(allRole[i], proc[0].say);
										break;
									}
								}
								
								//menu
								routeNotification(GameConstants.MODULE_INVOKE, ["-menu", ["normal", proc[0].child]], null, GameConstants.MODULE_ACTION + "/*");
								proc.shift();
								break;
							case "radio":
								//eventMediator.eventShell.initRadio(allRole[command.role - 1], command);
								break;
							case "move":
								routeNotification(GameConstants.GAME_CONSOLE, ["-gameCommand", ["RoomChange(" + proc[0].room + ");"]], null, "*");
								
								proc.shift();
								sendNotification(GameConstants.EVENT_RUN, ["play"]);
								break;
							case "role":
								var strName:String;
								
								switch(proc[0].name)
								{
									case "Status":
										strName = "身份";
										break;
									case "CampName":
										strName = "势力";
										break;
									case "OfficialRank":
										strName = "官阶";
										break;
									case "StaminaNum":
										strName = "体力";
										routeNotification(GameConstants.MODULE_INVOKE, ["-stamina", proc[0].value], null, GameConstants.MODULE_CHARACTER + "/*");
										break;
									case "EnergyNum":
										strName = "精力";
										routeNotification(GameConstants.MODULE_INVOKE, ["-energy", proc[0].value], null, GameConstants.MODULE_CHARACTER + "/*");
										break;
									case "ForceNum":
										strName = "武力";
										break;
									case "IntelligenceNum":
										strName = "智力";
										break;
									case "CommandNum":
										strName = "统率";
										break;
									case "PoliticsNum":
										strName = "政治";
										break;
									case "MeritNum":
										strName = "勋绩";
										break;
									case "ReputeNum":
										strName = "名声";
										break;
								}
								
								routeNotification(GameConstants.MODULE_INVOKE, ["-info", [1, "你的 " + strName + " 变更为：" + proc[0].value]], null, GameConstants.MODULE_PROMPT + "/*");
								
								proc.shift();
								sendNotification(GameConstants.EVENT_RUN, ["play"]);
								break;
							case "prompt":
								routeNotification(GameConstants.MODULE_INVOKE, ["-info", [proc[0].sort, proc[0].info]], null, GameConstants.MODULE_PROMPT + "/*");
								
								proc.shift();
								sendNotification(GameConstants.EVENT_RUN, ["play"]);
								break;
							case "shine":
								routeNotification(GameConstants.MODULE_INVOKE, ["-shine", [proc[0].sort, true]], null, GameConstants.MODULE_TOOL + "/*");
								
								proc.shift();
								sendNotification(GameConstants.EVENT_RUN, ["play"]);
								break;
							case "command":
								routeNotification(GameConstants.GAME_CONSOLE, ["-gameCommand", proc[0].cmd], null, "*");
								
								proc.shift();
								sendNotification(GameConstants.EVENT_RUN, ["play"]);
								break;
						}
					}
					else
					{
						eventMediator.eventShell.hidden();
						
						for (i = 0; i < systemData.getScript().length; i++)
						{
							if (systemData.curEvent == systemData.getScript()[i].Name)
							{
								systemData.getScript().splice(i, 1);
							}
						}
						
						systemData.curEvent = "";
						
						sendNotification(GameConstants.EVENT_RUN, ["start"]);
					}
					break;
			}
		}
		
		private function _onSoundLoaded(evt:Event):void
		{
			eventSound.play(0, 0, new SoundTransform(systemData.effectVol*.01));
			
			sendNotification(GameConstants.EVENT_RUN, ["play"]);
		}
	}
}